import java.awt.Graphics;
import java.awt.Rectangle;

public class Mob extends Rectangle {

	public int xC, yC;
	public int mobSize = 52;
	public int mobID = Value.mobAir;
	public boolean inGame = false;
	public int mobWalk = 0;
	public int upward = 0, downward = 1, right = 2, left = 3;
	public int directions = right;
	public boolean hasUpward = false;
	public boolean hasDownward = false;
	public boolean hasRight = false;
	public boolean hasLeft = false;
	

	public Mob() {

	}

	public void spawnMob(int mobID) {
		for (int y = 0; y < Screen.room.block.length; y++) {
			if (Screen.room.block[y][0].groundID == Value.groundRoad) {
				setBounds(Screen.room.block[y][0].x, Screen.room.block[y][0].y,
						mobSize, mobSize);
				xC = 0;
				yC = y;
			}
		}
		this.mobID = mobID;
		inGame = true;
	}

	public void draw(Graphics g) {
		// if(inGame) {
		g.drawImage(Screen.tileset_mob[mobID], x, y, width, height, null);
		// }
	}
	
	public void deleteMob() {
		inGame = false;	
	}
	
	public void looseHealth() {
		Screen.health -= 1;
	}

	public int walkFrame = 0, walkSpeed = 20;

	public void physic() {
		if (walkFrame >= walkSpeed) {
			if (directions == right) {
				x += 1;
			} else if (directions == upward) {
				y -= 1;
			} else if (directions == downward) {
				y += 1;
			} else if (directions == left) {
				x -= 1;
			}

			mobWalk += 1;

			if (mobWalk == Screen.room.blockSize) {
				if (directions == right) {
					xC += 1;
					hasRight = true;
				} else if (directions == upward) {
					yC -= 1;
					hasUpward = true;
				} else if (directions == downward) {
					yC += 1;
					hasDownward = true;
				} else if (directions == left) {
					xC -= 1;
					hasLeft = true;
				}

				if(!hasUpward) {
				try {
					if (Screen.room.block[yC + 1][xC].groundID == Value.groundRoad) {
						directions = downward;
					}
				} catch (Exception e) {

				}
				}
				
				
				if(!hasDownward) {
				try {
					if (Screen.room.block[yC - 1][xC].groundID == Value.groundRoad) {
						directions = upward;
					}
				} catch (Exception e) {

				}
				}
				
				if(!hasLeft) {
				try {
					if (Screen.room.block[yC][xC + 1].groundID == Value.groundRoad) {
						directions = right;
					}
				} catch (Exception e) {

				}
				}
				
				if(!hasRight) {
				try {
					if (Screen.room.block[yC][xC - 1].groundID == Value.groundRoad) {
						directions = left;
					}
				} catch (Exception e) {

				}
				}
				
				if (Screen.room.block[yC][xC].airID == Value.airCave) {
					deleteMob();
					looseHealth();
				}
				
				hasDownward = false;
				hasUpward = false;
				hasLeft = false;
				hasRight = false;
				mobWalk = 0;
			}

			walkFrame = 0;
		} else {
			walkFrame += 1;
		}
	}
}
